from django import template
from django.core.urlresolvers import reverse
import urllib, re

register = template.Library()

@register.simple_tag(takes_context=True)
def categorySearchURL(context):
  request = context['request']
  url = request.get_full_path()
  news_url = reverse("news");
  
  # If contains "news", we want to keep category
  if news_url in url:
    return url
  else:
    return news_url

@register.simple_tag(takes_context=True)
def url_category(context, view, param):
  request = context['request']
  get_part = '';
  url = request.get_full_path()
  if '?' in url:
    get_part = url.split('?')[1]
  if get_part != '':
    get_part = '?' + get_part

  if param == '':
    return reverse('news').lower() + get_part.lower()
  else:
    param = re.sub('[^A-Za-z0-9 ]+', '',param) # del all special chars
    return reverse(view, args=[ param.replace(" ", "_").encode("utf8") ]).lower() + get_part.lower()

@register.simple_tag(takes_context=True)
def is_active_category(context, param):
  # if module name contains param, then this should be active
  print param
  print "\n\n"
  print context['module_name'].lower()
  print "\n\n"
  if param.lower() in context['module_name'].lower():
    return 'class="active"'
  else:
    return '';
